package com.example.medicalzb.controller;

import com.example.medicalzb.param.CompanyPolicyParam;
import com.example.medicalzb.service.CompanyPolicyService;
import com.example.medicalzb.util.Msg;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/api/company_policys")
@CrossOrigin
@Api(tags = "公司政策控制器类")
public class CompanyPolicyController {
    @Autowired
    private CompanyPolicyService companyPolicyService;

    //查询公司政策信息
    @GetMapping(value = "")
    public Msg getPolicyWithPage(CompanyPolicyParam companyPolicyParam){
        return companyPolicyService.getAllPolicyWithPage(companyPolicyParam);
    }

    //添加公司政策
    @PostMapping(value = "")
    @RolesAllowed({"1"})
    public Msg saveMedicalPolicy(@RequestBody CompanyPolicyParam companyPolicyParam){
        return companyPolicyService.saveMedicalPolicy(companyPolicyParam);
    }

    //更新公司政策
    @PutMapping(value = "/{id}")
    @RolesAllowed({"1"})
    public Msg updateMedicalPolicy(@PathVariable("id") Long id,@RequestBody CompanyPolicyParam companyPolicyParam){
        if (companyPolicyParam.getTitle() == null || companyPolicyParam.getTitle().equals("")){
            return Msg.fail().mess("请输入标题");
        }
        if (companyPolicyParam.getMessage() ==null || companyPolicyParam.getMessage().equals("")){
            return Msg.fail().mess("请输入政策内容");
        }
        if (companyPolicyParam.getCompanyId() == null){
            return Msg.fail().mess("请选择公司");
        }
        return companyPolicyService.updateMedicalPolicy(id,companyPolicyParam);
    }

    //删除公司政策
    @DeleteMapping(value = "{id}")
    @RolesAllowed({"1"})
    public Msg deleteMedicalPolicy(@PathVariable("id") Long id){
        return companyPolicyService.deleteMedicalPolicy(id);
    }
}
